import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "垂直拖动屏幕",
      home: BottomDragWidget(),
    );
  }
}

class BottomDragWidget extends StatefulWidget {
  @override
  _BottomDragWidgetState createState() => _BottomDragWidgetState();
}

class _BottomDragWidgetState extends State<BottomDragWidget> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('垂直上拉抽屉'),
      ),
      body: Container(
        child: Stack(
          children: <Widget>[
            Align(
              alignment: Alignment.bottomCenter,
              child: DragContainer(),
            )
          ],
        ),
      ),
    );
  }
}

class DragContainer extends StatefulWidget {
  @override
  _DragContainerState createState() => _DragContainerState();
}

class _DragContainerState extends State<DragContainer> {
  double vertialDragDistance = 500.0;

  @override
  Widget build(BuildContext context) {
    vertialDragDistance = vertialDragDistance.clamp(0.0, 500.0);
    return Transform.translate(
      offset: Offset(0.0, vertialDragDistance),
      child: GestureDetector(
        child: Container(
          height: 520,
          padding: const EdgeInsets.only(top: 20.0),
          decoration: BoxDecoration(
            color: Colors.blue,
            borderRadius: BorderRadius.vertical(top: Radius.circular(10.0)),
          ),
          child: _list(),
        ),
        onVerticalDragUpdate: (DragUpdateDetails details) {
          setState(() {
            print(details.delta);
            vertialDragDistance += details.delta.dy;
            print(vertialDragDistance);
          });
        },
        onVerticalDragEnd: (DragEndDetails details) {
          if (vertialDragDistance >= 380) {
            setState(() {
              vertialDragDistance = 500;
            });
          } else if (vertialDragDistance < 200) {
            setState(() {
              vertialDragDistance = 0.0;
            });
          }
        },
      ),
    );
  }

  Widget _list() {
    return NotificationListener(
      onNotification: (notification) {
        switch (notification.runtimeType) {
          case ScrollStartNotification:
            print("开始滚动");
            break;
          case ScrollUpdateNotification:
            print("正在滚动");
            break;
          case ScrollEndNotification:
            print("滚动停止");
            if (vertialDragDistance >= 380) {
              setState(() {
                vertialDragDistance = 500;
              });
            } else if (vertialDragDistance < 200) {
              setState(() {
                vertialDragDistance = 0.0;
              });
            }
            break;
          case OverscrollNotification:
            print("滚动到边界");
            setState(() {
              vertialDragDistance += notification.dragDetails.delta.dy;
            });
            break;
        }
      },
      child: ListView.builder(
        itemCount: 100,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text("ListItem$index",
                style: TextStyle(color: Colors.white, fontSize: 24)),
          );
        },
      ),
    );
  }
}
